import { Component, ComponentType } from "../Component";
import { any } from "../../core/logic";
import { color } from "../../core/color";
import { Buffer } from "../../gpu/types/Buffer";
/**
 * 全局环境光
 * @class
 * @memberof w.light
 */
class AmbientLight extends Component {
    /**
     * @constructor
     * @param {String} opts.color 环境光颜色。
     * @param {Number} opts.intensity 环境光强度。
     * @param {*} share 共享参数
     * @example
     * let ambient=new w.light.Ambient({color:"#ffffff",intensity:1.0});
     */
    constructor(opts = {}, share) {
        opts.type = any(opts.type, ComponentType.AmbientLight);
        super(opts, share);
        this.color =any(opts.color,"#ffffff");
        this.intensity =any(opts.intensity,1.0);
        this.autoUpdate=true;
        this.update();
    }
    /**
     * @property {String} 环境光的颜色。
     */
    set color(v){
        this._color=v;
        this.addParam("ambient");
        this.update();
        return this;
    }
    get color(){
        return this._color;
    }
    /**
     * @property {Number} 环境光强度。
     */
    set intensity(v){
        this._intensity=v;
        this.addParam("ambient");
        this.update();
        return this;
    }
    get intensity(){
        return this._intensity;
    }

    toGPU() {
        if(this.has('ambient')){
            let lightGPUParameter = this.getParam('ambient');
            if(!this.getState("lightParameter")){
                let data = new Float32Array(4);
                data.set(color.format(this.color), 0);
                data.set([this.intensity], 3);
                if (lightGPUParameter) {
                    if(!lightGPUParameter.state) {
                        lightGPUParameter.buffer(data, 0);
                    }
                }else{
                    lightGPUParameter = new Buffer({
                        id: "ambient",
                        name: "ambientLight",
                        usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
                        visibility: GPUShaderStage.FRAGMENT,
                        type: "uniform",
                        size: 20,
                        data
                    });
                    this.addParam('ambient',lightGPUParameter);
                }
                lightGPUParameter.state = true;
            }
        }
    }
    updateState(){
        this.setState(true);
    }
}

export { AmbientLight }